package routers

import (
	"net/http"

	"gitee.com/cyanbleeze/go-image-management/global"
	"gitee.com/cyanbleeze/go-image-management/internal/middleware"
	"gitee.com/cyanbleeze/go-image-management/internal/routers/api"
	v1 "gitee.com/cyanbleeze/go-image-management/internal/routers/api/v1"
	"github.com/gin-gonic/gin"
)

func NewRouter() *gin.Engine {
	r := gin.New()
	r.Use(gin.Logger())
	r.Use(gin.Recovery())
	//并发请求过来时会有抢夺问题导致系统崩溃
	// r.Use(middleware.Translations())
	r.Use(middleware.Cors())

	picture := v1.NewPicture()
	user := v1.NewUser()

	token := r.Group("api/token")
	t := api.NewToken()
	token.POST("", t.Post)
	token.GET("", middleware.JWTAuthMiddleware(), t.Get)
	apiv1 := r.Group("api/v1")
	{
		pictures := apiv1.Group("pictures", middleware.JWTAuthMiddleware())
		pictures.POST("", picture.Create)
		pictures.DELETE("/:id", picture.Delete)
		pictures.PUT("/:id", picture.Update)
		pictures.PATCH("/:id/state", picture.Update)
		pictures.GET("", picture.List)
		pictures.GET("/:id", picture.Get)

		users := apiv1.Group("users")
		users.POST("", user.Create)
		users.Use(middleware.JWTAuthMiddleware())

		categories := apiv1.Group("categories", middleware.JWTAuthMiddleware())
		category := v1.NewCategory()
		categories.POST("", category.Create)
		categories.DELETE("/:id", category.Delete)
		categories.PUT("/:id", category.Update)
		categories.GET("", category.List)

		comments := apiv1.Group("comments", middleware.JWTAuthMiddleware())
		comment := v1.NewComment()
		comments.POST("", comment.Create)
		comments.DELETE("/:id", comment.Delete)
		comments.PUT("/:id", comment.Update)
		comments.GET("", comment.List)

		likes := apiv1.Group("likes", middleware.JWTAuthMiddleware())
		like := v1.NewLike()
		likes.PUT("", like.Update)
		likes.GET("", like.Get)

		favorites := apiv1.Group("favorites", middleware.JWTAuthMiddleware())
		favorite := v1.NewFavorite()
		favorites.POST("", favorite.Create)
		favorites.DELETE("/:id", favorite.Delete)
		favorites.PUT("/:id", favorite.Update)
		favorites.GET("", favorite.List)

		fps := apiv1.Group("favorite/picture", middleware.JWTAuthMiddleware())
		fp := v1.NewFavoirtePicture()
		fps.POST("", fp.Create)
		fps.DELETE("/:id", fp.Delete)
		fps.PUT("/:id", fp.Update)
		fps.GET("", fp.List)

		publics := apiv1.Group("public")
		public := v1.NewPublic()
		publics.GET("", public.List)
		publics.PUT("/:id", middleware.JWTAuthMiddleware(), public.Update)

		details := apiv1.Group("detail", middleware.JWTAuthButContinueMiddleware())
		detail := v1.NewDetail()
		details.GET("/:id", detail.Get)

	}

	u := api.NewUpload()
	r.POST("api/upload", middleware.JWTAuthMiddleware(), u.UploadFile)
	r.StaticFS("static", http.Dir(global.AppSetting.UploadSavePath))
	return r
}
